package com.infotel.prefecture.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.infotel.prefecture.exception.PrefectureException;
import com.infotel.prefecture.model.CarteGrise;

public class CarteGriseDAOJdbc extends AbstractDAOJdbc implements CarteGriseDAO {

    @Override
    public List<CarteGrise> findByImmatAndPuissance(String immat,
            int puissanceAdministrative) throws PrefectureException {
        Connection cx = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<CarteGrise> listCarteGrise = new ArrayList<CarteGrise>();
        try {
            cx = getConnection();
            if (immat.isEmpty()) {
                String sql = "select cgi_immatriculation, cgi_puiss_admin from carte_grise where cgi_puiss_admin = ?";
                ps = cx.prepareStatement(sql);
                ps.setInt(1, puissanceAdministrative);
                rs = ps.executeQuery();
            } else {
                String sql = "select cgi_immatriculation, cgi_puiss_admin from carte_grise where cgi_immatriculation like ? or cgi_puiss_admin = ?";
                ps = cx.prepareStatement(sql);
                ps.setString(1, immat + "%");
                ps.setInt(2, puissanceAdministrative);
                rs = ps.executeQuery();
            }
            while (rs.next()) {
                String immatComplete = rs.getString(1);
                int puissanceActuelle = rs.getInt(2);
                CarteGrise cg = new CarteGrise();
                cg.setImmatriculation(immatComplete);
                cg.setPuissanceAdministrative(puissanceActuelle);
                listCarteGrise.add(cg);

            }
        } catch (Exception e) {
            throw new PrefectureException("Echec recherche Carte Grise", e);
        } finally {
            close(cx, ps, rs);
        }
        return listCarteGrise;
    }

    @Override
    public void insertCarteGrise(CarteGrise carteGrise)
            throws PrefectureException {

        Connection cx = null;
        PreparedStatement ps = null;
        try {
            cx = getConnection();
            String sql = "insert into carte_grise (cgi_immatriculation, cgi_eta_code, cgi_veh_num_serie, cgi_pro_id, cgi_puiss_admin) values (?,?,?,?,?)";
            ps = cx.prepareStatement(sql);
            ps.setString(1, carteGrise.getImmatriculation());
            ps.setString(2, carteGrise.getEtat().name());
            ps.setString(3, carteGrise.getVehicule().getNumeroSerie());
            ps.setInt(4, carteGrise.getProprietaire().getId());
            ps.setInt(5, carteGrise.getPuissanceAdministrative());
           ps.executeUpdate();
        } catch (Exception e) {
            throw new PrefectureException("Echec insertion carte grise", e);
        } finally {
            close(cx, ps);
        }
        
    }

}
